﻿#include "shujujieshou.h"
#include "ui_shujujieshou.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>
#include <QIODevice>
#include <QTextEdit>
#include <QFile>
#include <QTextStream>
#include <QTextCodec>
#include <QSqlQuery>
#include <QDomDocument>
shujujieshou::shujujieshou(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::shujujieshou)
{
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF8"));
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF8"));
    ui->setupUi(this);
    ui->table->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->table->setEditTriggers(QAbstractItemView::NoEditTriggers);

    ui->name->setFocusPolicy(Qt::NoFocus);
    ui->id->setFocusPolicy(Qt::NoFocus);
    ui->unit->setFocusPolicy(Qt::NoFocus);
    ui->birth->setFocusPolicy(Qt::NoFocus);
    ui->sex->setFocusPolicy(Qt::NoFocus);
    ui->zhiwu->setFocusPolicy(Qt::NoFocus);
    ui->minzu->setFocusPolicy(Qt::NoFocus);
    ui->rudang->setFocusPolicy(Qt::NoFocus);
    ui->health->setFocusPolicy(Qt::NoFocus);
    ui->jiguan->setFocusPolicy(Qt::NoFocus);
    ui->joinwork->setFocusPolicy(Qt::NoFocus);
    //不点击数据无法点击按钮；

    ui->pushButton_2->setEnabled(false);
    ui->pushButton_4->setEnabled(false);

    //时间选择
//    ui->dateEdit->setDisplayFormat("yyyy-MM-dd");//设置输出格式 ; 开始时间格式为2000-01-01 样式
//    ui->dateEdit->setCalendarPopup(true);
    //UserImpowerDate = ui->dateEdit->text();//获取授权时间
    connect(ui->pushButton_2,SIGNAL(clicked(bool)),this,SLOT(pressBtn()));
}

shujujieshou::~shujujieshou()
{
    delete ui;
}
void shujujieshou::pressBtn()
{
    emit sendDate(ui->name->text());
}

void shujujieshou::on_pushButton_clicked()//导入文件
{
    QSqlQuery query;
    QString fileName = QFileDialog :: getOpenFileName(this, NULL,NULL, "*.xml");//打开xml文件
    qDebug()<<fileName;
    QFile file(fileName);

    //    //--打开文件成功
    //    if (file.open(QFile ::ReadOnly | QFile ::Text))
    //    {
    //        //qDebug()<<"1";
    //        QTextStream textStream(&file);
    //        while (!textStream.atEnd())
    //        {
    //            //---QtextEdit按行显示文件内容
    //            ui->showText->append(textStream.readLine());
    //        }
    //    }
    //    else	//---打开文件失败
    //    {
    //        /*
    //            information函数参数说明：
    //            函数原型
    //            QMessageBox::information(
    //            QWidget * parent,
    //            const QString & title,					//--- 标题
    //            const QString & text,					//---显示内容
    //            StandardButtons buttons = Ok,	//---OK按钮
    //            StandardButton defaultButton = NoButton)
    //            [static]
    //            */
    //        QMessageBox ::information(NULL, NULL, "open file error");
    //    }

    //打开文件成功后的操作

    //    QDomElement docElem = qDomDoc.documentElement();
    //    QDomNode nodeswitch=docElem.elementsByTagName("item");//红色的为标签名
    //    QDomElement elemnodeswitch=nodeswitch.toElement();
    //    string lie=qPrintable(elemnodeswitch.attribute("lie"));
    //    if(lie=="id")
    //    {
    //    qDebug()<<"succes";
    //    }

    //



    QDomDocument docXML;
    if (!file.open(QIODevice::ReadOnly))
    {
        return ;
    }

    if (!docXML.setContent(&file))
    {
        file.close();
        return;
    }
    file.close();

    QDomElement xmlRoot = docXML.documentElement(); //返回根节点，table节点
    QDomNode domNode = xmlRoot.firstChild(); //获得第一个子节点,hang节点

    //    QString Name = xmlRoot.tagName();
    //    qDebug()<<Name;
    while (!domNode.isNull())  //如果节点不空
    {
        //        QString tagName = domNode.toElement().tagName();  //节点元素名称，hang节点
        //        qDebug()<<tagName;

        int row=ui->table->rowCount();//当前table行数
        ui->table->insertRow(row);//增加一行table
        QDomElement domElement = domNode.toElement(); //转换为元素，注意元素和节点是两个数据结构，其实差不多
        //        QDomNodeList list = domElement.childNodes();
        //        for (int i = 0; i<list.count(); i++) //遍历子元素，count和size都可以用,可用于标签数计数
        //        {
        QDomNode node = domElement.firstChild();
        while (!node.isNull()) {


            if (node.toElement().tagName() == "name") {
                QString no=node.firstChild().toText().data();
                //                qDebug()<<no;
                ui->table->setItem(row,0,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "birth") {
                QString no=node.firstChild().toText().data();
                //                qDebug()<<no;
                ui->table->setItem(row,1,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "id") {
                QString no=node.firstChild().toText().data();
                //                qDebug()<<no;

                ui->table->setItem(row,2,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "sex") {
                QString no=node.firstChild().toText().data();
                //                qDebug()<<no;
                if(no=="1"){
                    no="男";

                }else if(no=="0")
                {
                    no="女";
                }
                ui->table->setItem(row,3,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "nuit") {
                QString no=node.firstChild().toText().data();
                //                qDebug()<<no;

                ui->table->setItem(row,4,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "zhiwu") {
                QString no=node.firstChild().toText().data();
                //                qDebug()<<no;

                ui->table->setItem(row,5,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "minzu") {
                QString no=node.firstChild().toText().data();
                //                qDebug()<<no;



                query.exec(QString("select Nation from NationCode where NationID ='%1'").arg(no));
                while(query.next())
                {
                    QString Nation=query.value(0).toString();
                    ui->table->setItem(row,6,new QTableWidgetItem(Nation));
                }


            }
            else if (node.toElement().tagName() == "rudang") {
                QString no=node.firstChild().toText().data();
                //                qDebug()<<no;

                ui->table->setItem(row,7,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "joinwork") {
                QString no=node.firstChild().toText().data();
                //                qDebug()<<no;

                ui->table->setItem(row,8,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "health") {
                QString no=node.firstChild().toText().data();
                //                qDebug()<<no;

                ui->table->setItem(row,9,new QTableWidgetItem(no));
            }
            else if (node.toElement().tagName() == "jiguan") {
                QString no=node.firstChild().toText().data();
                //                qDebug()<<no;

                ui->table->setItem(row,10,new QTableWidgetItem(no));
            }

            node=node.nextSibling();
        }
        //        }
        //下一个兄弟节点,nextSiblingElement()是下一个兄弟元素
        domNode = domNode.nextSibling();
    }
    file.close();


}

void shujujieshou::on_pushButton_4_clicked()//显示选中的干部信息
{
    //    ui->iddate->clear();//清除原有数据
    //    ui->namedate->clear();
    //    ui->numdate->clear();

    ui->name->clear();
    ui->id->clear();
    ui->unit->clear();
    ui->birth->clear();
    ui->sex->clear();
    ui->zhiwu->clear();
    ui->minzu->clear();
    ui->rudang->clear();
    ui->health->clear();
    ui->jiguan->clear();
    ui->joinwork->clear();

    int row = ui->table->currentRow();
    //        if(rowIndex!=-1)
    //        {
    //            ui->pushButton_2->setEnabled(true);
    //            ui->pushButton_4->setEnabled(true);
    //        }


    int i=0;
    QVariant name = ui->table->item(row,i)->text();
    QString namestring=name.toString();
    ui->name->setText(namestring);
    i++;
    QVariant birth = ui->table->item(row,i)->text();
    QString birthstring=birth.toString();
    ui->birth->setText(birthstring);
    i++;
    QVariant id = ui->table->item(row,i)->text();
    QString idstring=id.toString();
    ui->id->setText(idstring);
    i++;
    QVariant sex = ui->table->item(row,i)->text();
    QString sexstring=sex.toString();

    ui->sex->setText(sexstring);


    i++;
    QVariant unit = ui->table->item(row,i)->text();
    QString unitstring=unit.toString();
    ui->unit->setText(unitstring);
    i++;
    QVariant zhiwu = ui->table->item(row,i)->text();
    QString zhiwustring=zhiwu.toString();
    ui->zhiwu->setText(zhiwustring);
    i++;

    QVariant minzu = ui->table->item(row,i)->text();
    QString minzustring=minzu.toString();

    ui->minzu->setText(minzustring);
    i++;




    QVariant rudang = ui->table->item(row,i)->text();
    QString rudangstring=rudang.toString();
    ui->rudang->setText(rudangstring);
    i++;
    QVariant joinwork = ui->table->item(row,i)->text();
    QString joinworkstring=joinwork.toString();
    ui->joinwork->setText(joinworkstring);
    i++;
    QVariant health = ui->table->item(row,i)->text();
    QString healthworkstring=health.toString();
    ui->health->setText(healthworkstring);
    i++;
    QVariant jiguan = ui->table->item(row,i)->text();
    QString jiguanstring=jiguan.toString();
    ui->jiguan->setText(jiguanstring);






}

void shujujieshou::on_pushButton_2_clicked()//将选中的干部信息导入到数据库中
{
    QSqlQuery query;
    QSqlQuery changeminzu;
    QSqlQuery insertzhiwu;
    QSqlQuery warning;
    //    query.prepare("INSERT INTO CadreInfo(IDNum,CadreName,Phone)" "VALUES(:FaceData,:StrName,:Sijian)");
    //  query.prepare("INSERT INTO Ctest(IDNum,CadreName,Birth)" "VALUES(:ata,:StrName,:Sijian)");
    query.prepare("INSERT INTO Ctest(CadreName,Birth,IDNum,Sex,UnitID,NationID,PartyTime,JoinWorkTime,Health,NativePlace,Zhiwu)" "VALUES(:CadreName,:Birth,:IDNum,:Sex,:UnitID,:NationID,:PartyTime,:JoinWorkTime,:Health,:NativePlace,:Zhiwu)");

    insertzhiwu.prepare("INSERT INTO Post(PostName,IDNum,QualifyType)""VALUES(:name,:id,:type)");
    // insertzhiwu.prepare("INSERT INTO Post(PostID,IDNum)""VALUES(:post,:id)");



    int row=ui->table->currentRow();
    int i=0;

    QVariant name = ui->table->item(row,i)->text();
    QString namedate=name.toString();
    i++;
    QVariant birth = ui->table->item(row,i)->text();
    QString birthdate=birth.toString();
    i++;
    QVariant id = ui->table->item(row,i)->text();
    QString iddate=id.toString();
    i++;
    QVariant sex = ui->table->item(row,i)->text();
    QString sexdate=sex.toString();

    if(sexdate=="男"){
        sexdate="1";

    }else if(sexdate=="女")
    {
        sexdate="0";

    }
    i++;
    QVariant unit = ui->table->item(row,i)->text();
    QString unitdate=unit.toString();
    i++;

    //************************************
    QVariant zhiwu = ui->table->item(row,i)->text();
    QString zhiwudate=zhiwu.toString();
    //    QString zhiwudate="职务";

    //******************************
    i++;
    QVariant minzu = ui->table->item(row,i)->text();//民族换为代码

    QString minzudate=minzu.toString();
    QString minzucode;
    changeminzu.exec(QString("select NationID from NationCode where Nation ='%1'").arg(minzudate));
    while(changeminzu.next()){
        minzucode=changeminzu.value(0).toString();
    }

    i++;
    QVariant rudnag = ui->table->item(row,i)->text();
    QString rudnagdate=rudnag.toString();
    i++;
    QVariant joinwork = ui->table->item(row,i)->text();
    QString joinworkdate=joinwork.toString();
    i++;
    QVariant health = ui->table->item(row,i)->text();
    QString healthdate=health.toString();
    i++;
    QVariant jiguan = ui->table->item(row,i)->text();
    QString jiguandate=jiguan.toString();
    i++;
warning.exec(QString("select CadreName from Ctest where IDNum='%1'").arg(iddate));
while(warning.next()){
    //QString name= warning.value(0).toString();
    jinggao =new waityihui;
    jinggao->show();
    }
    query.bindValue(":CadreName",namedate);//this->ui->iddate->text()
    query.bindValue(":Birth",birthdate);//this->ui->iddate->text()
    query.bindValue(":IDNum",iddate);//this->ui->iddate->text()
    query.bindValue(":Sex",sexdate);//this->ui->iddate->text()
    query.bindValue(":UnitID",unitdate);//this->ui->iddate->text()
    query.bindValue(":Zhiwu",zhiwudate);//this->ui->iddate->text()
    query.bindValue(":NationID",minzucode);//this->ui->iddate->text()//
    query.bindValue(":PartyTime",rudnagdate);//this->ui->iddate->text()
    query.bindValue(":JoinWorkTime",joinworkdate);//this->ui->iddate->text()
    query.bindValue(":Health",healthdate);//this->ui->iddate->text()
    query.bindValue(":NativePlace",jiguandate);//this->ui->iddate->text()

    //insertzhiwu.bindValue(":post",1);//this->ui->iddate->text()
    insertzhiwu.bindValue(":name",zhiwudate);//this->ui->iddate->text()
    insertzhiwu.bindValue(":id",iddate);//this->ui->iddate->text()
    insertzhiwu.bindValue(":type",0);//this->ui->iddate->text()

    //    query.bindValue(":ata",12);//this->ui->iddate->text()//一定要有冒号：
    //    query.bindValue(":StrName",jiguanstring);
    //    query.bindValue(":Sijian",this->ui->dateEdit->text());
    //    query.bindValue(":FaceData",11);//this->ui->iddate->text()
    //    query.bindValue(":StrName",this->ui->namedate->text());
    //    query.bindValue(":Sijian",this->ui->numdate->text());

    query.exec();
    insertzhiwu.exec();


}

void shujujieshou::on_table_clicked()//解锁按钮
{
    ui->pushButton_2->setEnabled(true);
    ui->pushButton_4->setEnabled(true);
}

void shujujieshou::on_pushButton_3_clicked()//添加全部干部信息
{
    QSqlQuery query;
    QSqlQuery changeminzu;
    QSqlQuery insertzhiwu;
    int row=ui->table->rowCount();//table的行
    //int lie=ui->table->columnCount();//table的列
    int j;
    //这里用Ctest表测试数据
    query.prepare("INSERT INTO Ctest(CadreName,Birth,IDNum,Sex,UnitID,NationID,PartyTime,JoinWorkTime,Health,NativePlace,Zhiwu)" "VALUES(:CadreName,:Birth,:IDNum,:Sex,:UnitID,:NationID,:PartyTime,:JoinWorkTime,:Health,:NativePlace,:Zhiwu)");

    insertzhiwu.prepare("INSERT INTO Post(PostName,IDNum,QualifyType)""VALUES(:name,:id,:type)");


    for(int i=0;i<row;i++){

        //    query.prepare("INSERT INTO CadreInfo(IDNum,CadreName,Phone)" "VALUES(:FaceData,:StrName,:Sijian)");
        // query.prepare("INSERT INTO CadreInfo(CadreName,Birth,IDNum,Sex,UnitID,NationID,PartyTime,JoinWorkTime,Health,NativePlace)" "VALUES(:CadreName,:Birth,:IDNum,:Sex,:UnitID,:NationID,:PartyTime,:JoinWorkTime,:Health,:NativePlace)");


        j=0;

        QVariant name = ui->table->item(i,j)->text();
        QString namedate=name.toString();
        j++;
        QVariant birth = ui->table->item(i,j)->text();
        QString birthdate=birth.toString();
        j++;
        QVariant id = ui->table->item(i,j)->text();
        QString iddate=id.toString();
        j++;
        QVariant sex = ui->table->item(i,j)->text();
        QString sexdate=sex.toString();

        if(sexdate=="男"){
            sexdate="1";

        }else if(sexdate=="女")
        {
            sexdate="0";

        }
        j++;
        QVariant unit = ui->table->item(i,j)->text();
        QString unitdate=unit.toString();
        j++;

        //************************************



        QVariant zhiwu = ui->table->item(i,j)->text();
        QString zhiwudate=zhiwu.toString();
        //    QString zhiwudate="职务";

        //******************************
        j++;
        QVariant minzu = ui->table->item(i,j)->text();
        QString minzudate=minzu.toString();
        QString minzucode;
        changeminzu.exec(QString("select NationID from NationCode where Nation ='%1'").arg(minzudate));
        while(changeminzu.next()){
            minzucode=changeminzu.value(0).toString();
        }


        j++;
        QVariant rudnag = ui->table->item(i,j)->text();
        QString rudnagdate=rudnag.toString();
        j++;
        QVariant joinwork = ui->table->item(i,j)->text();
        QString joinworkdate=joinwork.toString();
        j++;
        QVariant health = ui->table->item(i,j)->text();
        QString healthdate=health.toString();
        j++;
        QVariant jiguan = ui->table->item(i,j)->text();
        QString jiguandate=jiguan.toString();




        query.bindValue(":CadreName",namedate);//this->ui->iddate->text()
        query.bindValue(":Birth",birthdate);//this->ui->iddate->text()
        query.bindValue(":IDNum",iddate);//this->ui->iddate->text()
        query.bindValue(":Sex",sexdate);//this->ui->iddate->text()
        query.bindValue(":UnitID",unitdate);//this->ui->iddate->text()
        query.bindValue(":Zhiwu",zhiwudate);//this->ui->iddate->text()

        query.bindValue(":NationID",minzucode);//this->ui->iddate->text()
        query.bindValue(":PartyTime",rudnagdate);//this->ui->iddate->text()
        query.bindValue(":JoinWorkTime",joinworkdate);//this->ui->iddate->text()
        query.bindValue(":Health",healthdate);//this->ui->iddate->text()
        query.bindValue(":NativePlace",jiguandate);//this->ui->iddate->text()

        insertzhiwu.bindValue(":name",zhiwudate);//this->ui->iddate->text()
        insertzhiwu.bindValue(":id",iddate);//this->ui->iddate->text()
        insertzhiwu.bindValue(":type",0);//this->ui->iddate->text()


        // query.bindValue(":Data",12);//this->ui->iddate->text()//职务










        //    query.bindValue(":Data",12);//this->ui->iddate->text()
        //    query.bindValue(":StrName","haah");
        //    query.bindValue(":Sijian",this->ui->dateEdit->text());

        //    query.bindValue(":FaceData",11);//this->ui->iddate->text()
        //    query.bindValue(":StrName",this->ui->namedate->text());
        //    query.bindValue(":Sijian",this->ui->numdate->text());

        query.exec();
        insertzhiwu.exec();





    }

















}
